from django.shortcuts import render

# Create your views here.
from django.shortcuts import render
# 导入吐槽序列化器
from .spit_serializers import TalkSerializers
from rest_framework import status
# 导入模型类
import json
from .models import Spit
from rest_framework.generics import ListAPIView,RetrieveAPIView,GenericAPIView,UpdateAPIView
from rest_framework.viewsets import ModelViewSet
from .paginations import MyPage
from mongoengine import connect
from django.views import View
from django.http import HttpResponse,JsonResponse
from rest_framework.response import Response
class ScfView(View):
    def post(self,request):
        data = json.loads(request.body.decode())
        cox = data.get('cox')
        datatime=data.get('datatime')
        id = data.get('id')
        name = data.get('name')
        try:
           result = Spit.objects.create(content=cox, publishtime=datatime,
                            userid=id,nickname=name)  # 新增数据
        except Spit.DoseNotExist as e:
            return HttpResponse({
                '新增数据错误'
            })
        return HttpResponse("mongodb")

class SpitTalkView(ModelViewSet):
    # 指定模型类对象
    queryset = Spit.objects.all().order_by('thumbup')
    # 指定序列化器
    serializer_class = TalkSerializers
    # 指定自定义的分页器
    pagination_class = MyPage

#　根据id获取单个吐槽详情
class SpitId(RetrieveAPIView):
    queryset = Spit.objects.all()
    serializer_class = TalkSerializers
    # RetrieveAPIView中默认提取路劲参数是pk,但是前端传过来是id,故我们需要重新写提取值为id
    lookup_url_kwarg = 'id'

    def get_queryset(self):
        # 根据路径参数获取id
        id = self.kwargs.get('id')
        #　根据获取到的id和数据库中＿id相等获取数据查询集
        data = self.queryset.filter(id=id)
        return data


class SpitChildren(ListAPIView):
    queryset = Spit.objects.all()
    serializer_class = TalkSerializers


class SpitUpdatehumbup(UpdateAPIView):
    lookup_url_kwarg = 'id'
    serializer_class = TalkSerializers
    queryset = Spit.objects.all()
    def put(self,request,id):
         # 根据用户输入的数据,查询字符串参数,删除指定的书籍信息
         try:
             spit = Spit.objects.get(id=id)
         except Spit.DoesNotExist as e:
             return JsonResponse({'errmsg':'资源找不到'},status=404)
         if spit.hasthumbup:
             thumbup = spit.thumbup
             new_thumbup = thumbup - 1
             spit.thumbup = new_thumbup
             spit.hasthumbup = False
             spit.save()
             return JsonResponse({
                 'message': '取消点赞成功',
                 'success': True
             })
         else:
             thumbup = spit.thumbup
             new_thumbup = thumbup + 1
             spit.thumbup = new_thumbup
             spit.hasthumbup = True
             spit.save()
             return JsonResponse({
                 'message':'点赞成功',
                 'success':True
             })


class SpitCollected(UpdateAPIView):
    lookup_url_kwarg = 'id'
    serializer_class = TalkSerializers
    queryset = Spit.objects.all()

    def put(self, request, id):
        # 根据用户输入的数据,查询字符串参数,删除指定的书籍信息
        try:
            spit = Spit.objects.get(id=id)
        except Spit.DoesNotExist as e:
            return JsonResponse({'errmsg': '资源找不到'}, status=404)
        if spit.collected:
            spit.collected = False
            spit.save()
            return JsonResponse({
                'message': '取消收藏成功',
                'success': True
            })

        else:
            spit.collected = True
            spit.save()
            return JsonResponse({
                'message': '收藏成功',
                'success': True
            })